package com.service.impl;

import com.Dto.AllCounselorInfoDto;
import com.Dto.AllSupervisorInfoDto;
import com.Dto.CounselorToAddDto;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mapper.*;
import com.pojo.Counselor;
import com.pojo.Supervisor;
import com.pojo.User;
import com.pojo.Relationship;
import com.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

@Service
public class AdminServiceImpl extends ServiceImpl<UserMapper, User> implements AdminService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private AllSupervisorInfoMapper allSupervisorInfoMapper;

    @Autowired
    private AllCounselorInfoMapper allCounselorInfoMapper;

    @Autowired
    private RelationshipMapper relationshipMapper;

    @Autowired
    private CounselorMapper counselorMapper;

    @Autowired
    private SupervisorMapper supervisorMapper;


    //禁用用户
    @Override
    public boolean disableUserById(int userId) {
        int rowsAffected = userMapper.disableUserById(userId);
        return rowsAffected > 0;
    }

    //解除禁用
    @Override
    public boolean enableUserById(int userId) {
        int rowsAffected = userMapper.enableUserById(userId);
        return rowsAffected > 0;
    }

    //查看所有督导，包括督导姓名，督导名下的咨询师姓名，援助总次数，援助总时长，本月排班日期
    @Override
    public List<AllSupervisorInfoDto> findAllSupervisorInfo() {
        List<AllSupervisorInfoDto> allSupervisorsInfo = allSupervisorInfoMapper.findAllSupervisorInfo();
        return allSupervisorsInfo;
    }

    //查看所有咨询师信息，包括咨询师姓名，咨询师绑定的督导姓名，咨询总次数，咨询总时长，本月排班日期
    @Override
    public List<AllCounselorInfoDto> findAllCounselorInfo() {
        List<AllCounselorInfoDto> allCounselorsInfo = allCounselorInfoMapper.findAllCounselorInfoDto();
        return allCounselorsInfo;
    }

    //添加咨询师

    @Transactional
    public boolean addCounselor(CounselorToAddDto counselorToAdd) {

        try {

            // 创建 User 对象并插入 user 表
            User user = new User();
            user.setUserName(counselorToAdd.getUserName());
            user.setPassword(counselorToAdd.getPassword());
            user.setRoleId(1);
            userMapper.insert(user);

            //创建Counselor对象并插入counselor表
            Counselor counselor = new Counselor();//插入相关信息,姓名 性别 年龄  身份证号码 电话号码 邮箱 职称 工作单位
            counselor.setName(counselorToAdd.getName());
            counselor.setGender(counselorToAdd.getGender());
            counselor.setAge(counselorToAdd.getAge());
            counselor.setIdNumber(counselorToAdd.getIdNumber());
            counselor.setPhoneNumber(counselorToAdd.getPhoneNumber());
            counselor.setEmail(counselorToAdd.getEmail());
            counselor.setUserId(user.getId());
            counselor.setTitle(counselorToAdd.getTitle());
            counselor.setDepartment(counselorToAdd.getDepartment());
            counselorMapper.insert(counselor);
            // 获取插入 counselor 表后生成的主键
            Integer counselorId = counselor.getId();

            // 创建 Relationship 对象并插入 Relationship 表
            Relationship relationship = new Relationship();
            //找到绑定督导的id
            relationship.setCounselorId(counselorId);
            Supervisor supervisor = supervisorMapper.findSupervisorByUserId(counselorToAdd.getSupervisorBoundId());
            relationship.setSupervisorId(supervisor.getId());
            relationshipMapper.insert(relationship);
            return true;
        } catch (Exception e) {
            // 发生异常时，回滚事务
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            throw e;  // 抛出异常，通知调用者事务执行失败
        }
    }

    @Override
    public boolean addRelationship(int counselorUserId, int supervisorUserId) {
        Counselor counselor = counselorMapper.findCounselorByUserId(counselorUserId);
        Supervisor supervisor = supervisorMapper.findSupervisorByUserId(supervisorUserId);
        int counselorId = counselor.getId();
        int supervisorId = supervisor.getId();
        boolean action = relationshipMapper.insertRelationship(counselorId, supervisorId);
        return  action;
    }
}


